package com.contract.spirit.ui.income

import android.content.Context
import android.content.Intent
import com.contract.commonlib.util.ext.setOnSingleClickListener
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.refresh.IRefreshCallBack
import com.contract.commonlib.refresh.RefreshManager
import com.contract.commonlib.util.RecycleviewLayoutManger
import com.contract.spirit.R
import com.contract.spirit.databinding.ActivityIncomeDetailsBinding
import com.contract.spirit.manage.CommonConfigManager
import com.contract.spirit.ui.password.SetPasswordActivity
import com.contract.spirit.ui.withdraw.WithdrawCoinActivity

/**
 * @author: witness
 * created: 2021/9/28
 * desc:
 */
class IncomeDetailsActivity : BaseActivity<ActivityIncomeDetailsBinding, IncomeViewModel>() {
    private var pageNum: Int = 1

    private val recordAdapter by lazy {
        IncomeRecordAdapter()
    }

    override fun setContentView(): Int {
        return R.layout.activity_income_details
    }

    override fun initData() {
        RecycleviewLayoutManger.setVerticalManager(mBinding.rvRecord, mContext)
        recordAdapter.addFooterView(footView)
        mBinding.rvRecord.adapter = recordAdapter
        mBinding.unCashAmount = CommonConfigManager.userInfoResponse.unCashAmount
        mBinding.vgWithdraw.setOnSingleClickListener {
            if (CommonConfigManager.userInfoResponse.isSetPassword()) {
                 WithdrawCoinActivity.launch(mContext)
            } else {
                 SetPasswordActivity.launch(mContext)
            }
        }
        getRecordInfo()
    }

    override fun onReload() {
        super.onReload()
        mViewModel.incomeDetaiRes.value = null
        getRecordInfo()
    }

    override fun initRefresh() {
        RefreshManager.getInstance()
            .initRefresh(mContext, footView, true, mBinding.refresh, object : IRefreshCallBack {
                override fun onRefresh() {
                    pageNum = 1
                    getRecordInfo()
                }

                override fun onLoadMore() {
                    pageNum++
                    getRecordInfo()
                }
            })
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.incomeDetaiRes.observe(this, { it ->
            parseState(it, {
                if (pageNum == 1) {
                    recordAdapter.setList(it)
                } else {
                    recordAdapter.addData(it)
                }
                setDataLoadFinish(pageNum, it.size, mBinding.refresh)
            }, {
                endLoad(pageNum, mBinding.refresh)
            })
        })
    }

    private fun getRecordInfo() {
        mViewModel.accoutDetailList(pageNum)
    }

    companion object {
        fun launch(mContext: Context) {
            mContext.startActivity(Intent(mContext, IncomeDetailsActivity::class.java))
        }
    }
}